package com.eascs.crm.nsEditDetail.controller;

import java.text.Format;
import java.util.Date;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.nsEditDetail.entity.OtherInfoEntity;
import com.eascs.crm.nsEditDetail.entity.OtherInfoLogEntity;
import com.eascs.crm.nsEditDetail.form.OtherInfoForm;
import com.eascs.crm.nsEditDetail.form.OtherLogInfoForm;
import com.eascs.crm.nsEditDetail.service.OtherInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.UserHolder;

/**
 * 夫妻双方其他资产信息
 * 
 * @author lwx
 *
 */
@Controller
@RequestMapping("/other_assets")
public class OtherInfoRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(OtherInfoRestController.class);

	@Inject
	OtherInfoService otherInfoService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;

	@RequestMapping("/other_assets_list")
	public PaginationRestResult house_list(String loanapplyId,@RequestParam(defaultValue="0") Integer showPage,String type) {
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyId);
		PageResult<Object[]> pageResult = otherInfoService.findCarListInfo(loanapplyId,showPage);
		String[] oatitles = {"资产类型","信贷经理估值（万元）" };
		String o2ourl = "/other_assets/other_assets_list";
		if (StringUtil.isNotBlank(loanapplyId)) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanapplyId", loanapplyId);
			o2ourl = o2ourl + "&showPage=";
		} else {
			o2ourl = o2ourl + "?showPage=";
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, oatitles, o2ourl);
		Button editButton = null;
		Button delButton = null;
		if(!"detail".equals(type)){
			if(StatusContexts.O2O_ZX_END.equals(o2oLoanapplyEntity.getWfType())){
				editButton = ButtonHelper.createAjaxButton("修改", "/other_assets/other_assets_op?otherId=", "UpdateInternalInfo");
				delButton = ButtonHelper.createAjaxButton("  删除 ", "/other_assets/other_assets_del?otherId=", "UpdateInternalInfo");
			}
		}
		Button[] detailButton = new Button[] { editButton, delButton };
		SelectDataOption[] selectDataOptions = {new SelectDataOption("ZCLX"), null,null, null};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_other_assets");
		paginationInfo.setFormats(new Format[] { null, null, null});
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	// 新增修改
	@RequestMapping(value = "/other_assets_op")
	public HtmlRestResult houseOp(String loanapplyId,String otherId) {
		OtherInfoEntity vo = new OtherInfoEntity();
		if (StringUtil.isNotBlank(otherId)) {
			vo = otherInfoService.findOtherInfoEntity(otherId);
		}
		ViewContext.put("loanapplyId", loanapplyId);
		ViewContext.put("vo", vo);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/other_assets/other_assets_op");
	}

	@RequestMapping(value = "/other_assets_del")
	public BaseRestResult<String> houseDel(String otherId) {
		try {
			int i =otherInfoService.otherDel(otherId);
			if(i>0){
				return BaseRestResult.info("操作成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_other_assets");
			}else{
				return BaseRestResult.info("操作失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_other_assets");
			}
		} catch (Exception e) {
			return BaseRestResult.warn("数据库操作异常");
		}
	}

	@RequestMapping("/other_assets_save")
	public BaseRestResult<String> houseSave(OtherInfoForm form) {
		int flag=0;
		if (form != null && StringUtil.isNotBlank(form.getOtherId())) {// 修改
			OtherInfoEntity other = otherInfoService.findOtherInfoEntity(form.getOtherId());
			try {
				flag=otherInfoService.otherUpdate(other, form);
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		} else {// 新增
			OtherInfoEntity other = new OtherInfoEntity();
			try {
				flag=otherInfoService.otherSave(other, form);
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		}
		if(flag>0){
			return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_other_assets");
		}else{
			return BaseRestResult.info("操作失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_other_assets");
		}
	}

	@RequestMapping("/other_assets_list_form")
	public PaginationRestResult house_list_form(String loanapplyId,@RequestParam(defaultValue="0") Integer showPage) {
		PageResult<Object[]> pageResult = otherInfoService.findCarListInfo(loanapplyId,showPage);
		String[] oatitles = {"资产类型","信贷经理估值（万元）", "估值复核（万元）" };
		String o2ourl = "/other_assets/other_assets_list_form";
		if (StringUtil.isNotBlank(loanapplyId)) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanapplyId", loanapplyId);
			o2ourl = o2ourl + "&showPage=";
		} else {
			o2ourl = o2ourl + "?showPage=";
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, oatitles, o2ourl);
		AjaxButton editButton = new AjaxButton("修改", "/other_assets/ns_other_view?otherId=");
		Button[] detailButton = new Button[] { editButton };
		SelectDataOption[] selectDataOptions = {new SelectDataOption("ZCLX"), null,null, null};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_other_form");
		paginationInfo.setFormats(new Format[] { null, null, null});
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
	
	@RequestMapping(value = "/ns_other_view")
	public HtmlRestResult nsOtherView(String otherId) {
		OtherInfoEntity vo = new OtherInfoEntity();
		OtherInfoLogEntity log =new OtherInfoLogEntity();
		if (StringUtil.isNotBlank(otherId)) {
			vo = otherInfoService.findOtherInfoEntity(otherId);
			log = otherInfoService.findOtherLogEntity(otherId);
		}
		ViewContext.put("vo", vo);
		ViewContext.put("log", log);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/other_assets/other_assets_op_form");
	}
	
	@RequestMapping("/other_form_save")
	public BaseRestResult<String> otherFormSave(OtherLogInfoForm form) {
		OtherInfoLogEntity log = new OtherInfoLogEntity();
		log.setOtherInfoId(form.getOtherId());
		log.setOpPsn(UserHolder.getUser().getCname());
		log.setOpNo(UserHolder.getUser().getCode());
		log.setOpDate(new Date());
		log.setOtherPrice(form.getOtherPriceReview());
		int i = otherInfoService.insert(log);
		if(i>0){
			return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_other_form");
		}else{
			return BaseRestResult.warn("操作失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_other_form");
		}
	}
}
